<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="选择图片" prop="img">
      <!-- <el-input v-model="ruleForm.img" autocomplete="off"></el-input> -->
      <el-upload class="avatar-uploader" action="http://114.116.29.103:8089/upload" :show-file-list="false"
        :on-success="handleAvatarSuccess">
        <img v-if="ruleForm.img" :src="ruleForm.img" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="店名" prop="link">
      <el-input v-model="ruleForm.link" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="门店地址" prop="alt">
      <el-input v-model="ruleForm.alt"></el-input>
    </el-form-item>
    <el-form-item label="营业时间" prop="time">
      <el-input v-model="ruleForm.time"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="contact">
      <el-input v-model="ruleForm.contact"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('门店地址不能为空'));
      } else {
        callback();
      }

    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('店名图片不能为空'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('店名不能为空'));
      } else {
        callback();
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('营业时间不能为空'));
      } else {
        callback();
      }
    };
    var validatePass4 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('联系电话不能为空'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        img: '',
        link: '',
        alt: '',
        time:'',
        contact:''
      },
      rules: {
        img: [
          { validator: validatePass, trigger: 'blur' }
        ],
        link: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        alt: [
          { validator: checkAge, trigger: 'blur' }
        ],
        time: [
          { validator: validatePass3, trigger: 'blur' }
        ],
        contact:[
          { validator: validatePass4, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    handleAvatarSuccess(res){
      console.log(res);
      this.ruleForm.img=res.data[0]
    },
 
    resetForm(formName) {
      // this.ruleForm.img=''
      // this.ruleForm.link=''
      // this.ruleForm.alt=''
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  
</style>
